package com.example.demo.utils.smsUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SMSUtils {

	private static final Logger logger = LoggerFactory.getLogger(SMSUtils.class);

	public static boolean sendSMS(String text, String mobile) {
		logger.info("发送短信手机：" + mobile + "-内容：" + text);
		// 获取配置参数
		boolean sendFlag = true; // 发送状态
		try {
			// 建立连接
			URL url = new URL("http://10.1.32.232:809/Service1.asmx/SendSms");
			URLConnection connection = url.openConnection();
			connection.setDoOutput(true);
			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
			// 发送参数、短信内容
			StringBuffer sb = new StringBuffer();
			sb.append("login_name=").append("test").append("&password=").append("test123").append("&mobile=")
					.append(mobile).append("&message=").append(text).append("&start_time=&Search_ID=");
			out.write(sb.toString());
			out.flush();
			out.close();
			String sTotalString = "";
			String sCurrentLine = "";
			InputStream l_urlStream = connection.getInputStream();
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString += sCurrentLine + "\r\n";
			}
			if (out != null) {
				out.close();
			}
			if (l_urlStream != null) {
				l_urlStream.close();
			}
			if (l_reader != null) {
				l_reader.close();
			}
		} catch (Exception e) {
			sendFlag = false;
			logger.error("发送短信异常",e);
		}
		logger.info("发送短信成功手机：" + mobile);
		return sendFlag;
	}
}
